package com.dzsw.thread;

/**
 * @author sunz
 * @version 1.0
 * @CreateTime 2021-04-20-13:09
 */
public class RunningMain {

    public static void main(String[] args) throws Exception {
        MyThread1 exThread = new MyThread1();
        exThread.start();
        Thread.sleep(2 * 1000);
        exThread.interrupt();
        exThread.join();
        System.out.println("end.");
    }
}

class MyThread1 extends Thread {

    @Override
    public void run() {
        HelloThread1 helloThread = new HelloThread1();
        helloThread.start();
        try {
            helloThread.join();
        } catch (InterruptedException e) {
            System.out.println("interrupted!");
        }
        helloThread.stopThread();
    }
}

class HelloThread1 extends Thread {

    private volatile boolean running = true;

    @Override
    public void run() {
        int n = 0;
        while(running) {
            System.out.println("Hello, " + n++);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("interrupted!sleep!");
//                break;
            }
            System.out.println("running = " + running);
        }
    }

    public void stopThread() {
        this.running = false;
    }
}
